﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ProjectRoughWork.Engine.SoundManager;

namespace ProjectRoughWork
{
    class ResultScreen : GameScreen
    {
        public ResultScreen(Squad initPlayerSquad)
            : base()
        {
            //Retrieve the width and height of the screen 

            int width = Game1.GetGraphicsDevice().Viewport.Width;
            int height = Game1.GetGraphicsDevice().Viewport.Height;

            //Retrieve necessary graphics tools

            spriteBatch = Game1.GetSpriteBatch();
            headerFont = Game1.GetHeaderFont();
            spriteFont = Game1.GetSpriteFont();

            // Play our level complete music
            SoundManager.GetInstance().playBackgroundMusic(SoundManager.BGM_MENU_LEVEL_COMPLETE);

            //Store player squad

            playerSquad = initPlayerSquad;

            //Set up header text variables

            header = "Results";
            headerOrigin = headerFont.MeasureString(header);
            headerPos.X = width / 2 - headerOrigin.X / 2;
            headerPos.Y = height / 10 + headerOrigin.Y / 2;

            //Set up squad score variables

            Vector2 initPos = new Vector2(width/2, headerPos.Y + 2*headerOrigin.Y);
            squadScores = new string[3];
            squadScorePos = new Vector2[3];
            squadScoreOrigin = new Vector2[3];

            for (int index = 0; index < 3; index++)
            {
                if (playerSquad.SquadUnits[index].UnitType.Equals("Soldier"))
                {
                    squadScores[index] = "Unit " + index + " Soldier XP: " + playerSquad.SquadUnits[index].XP;
                }
                else if (playerSquad.SquadUnits[index].UnitType.Equals("Skirmisher"))
                {
                    squadScores[index] = "Unit " + index + " Skirmisher XP: " + playerSquad.SquadUnits[index].XP;
                }
                else if (playerSquad.SquadUnits[index].UnitType.Equals("Demolition"))
                {
                    squadScores[index] = "Unit " + index + " Demolition XP: " + playerSquad.SquadUnits[index].XP;
                }

                squadScoreOrigin[index] = spriteFont.MeasureString(squadScores[index]);
                squadScorePos[index].X = initPos.X - squadScoreOrigin[index].X / 2;
                squadScorePos[index].Y = initPos.Y + squadScoreOrigin[index].Y / 2;

                initPos.Y += squadScoreOrigin[index].Y;
            }
        }

        public override void Draw()
        {
            //Make background black

            Game1.GetGraphicsDevice().Clear(Color.Black);

            spriteBatch.Begin();

            //Draw header text

            spriteBatch.DrawString(headerFont, header, headerPos, Color.Red);

            for (int index = 0; index < 3; index++)
            {
                //Draw Squad Scores

                spriteBatch.DrawString(spriteFont, squadScores[index], squadScorePos[index], Color.Red);
            }

            spriteBatch.End();

            base.Draw();
        }

        //Data Members
        Squad playerSquad;

        SpriteBatch spriteBatch;
        SpriteFont headerFont;
        SpriteFont spriteFont;

        string header;
        Vector2 headerPos;
        Vector2 headerOrigin;
        
        string[] squadScores;
        Vector2[] squadScorePos;
        Vector2[] squadScoreOrigin;
    }
}
